# Copyright 1999-2012 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: net-misc/xrdp/xrdp-0.6.0-r2.ebuild,v 1.2 2012/09/07 20:30:00 itspec.ru Exp $

EAPI="3"

inherit eutils autotools multilib

if [[ ${PV} == "9999" ]] ; then
    inherit git
    EGIT_REPO_URI="git://github.com/FreeRDP/xrdp.git"
    SRC_URI=""
    KEYWORDS="~x86"
else
    SRC_URI="mirror://sourceforge/${PN}/${PN}-v${PV}.tar.gz"
    KEYWORDS="~amd64 ~ppc ~sparc ~x86"
    S="${WORKDIR}/${PN}-v${PV}"
fi

DESCRIPTION="An open source remote desktop protocol(rdp) server."
HOMEPAGE="http://www.xrdp.org/"

LICENSE="Apache-2.0"
SLOT="0"

IUSE="+vnc x11rdp debug"

DEPEND="sys-libs/pam"
RDEPEND="${DEPEND}
    vnc? ( net-misc/tigervnc )
    x11rdp? ( >=x11-base/xorg-x11rdp-0.7.0.9999 )
    "
RESTRICT="${RESTRICT}
    debug? ( strip )
    "

pkg_setup() {
    einfo "checking for necessary groups and users...  create if missing.\n"
    enewgroup tsusers  || die "problem adding group tsusers"
    enewgroup tsadmins || die "problem adding group tsadmins"
}

src_unpack() {
    if [[ ${PV} == "9999" ]] ; then
        git_src_unpack
    else
        unpack ${A}
    fi
}

src_prepare() {
    cp ${FILESDIR}/startwm.sh ./sesman/
    sed -e 's:/usr/local/sbin:/usr/sbin:' -i ${S}/instfiles/xrdp.sh
    ln -s ../config.c ${S}/sesman/tools/config.c
    eautoreconf
}

src_configure() {
    econf \
        --prefix=/usr \
        --sysconfdir=/etc \
        --localstatedir=/var \
        $(use_enable debug xrdpdebug) \
        || die "econf failed"
}

src_compile() {
    if use debug; then
        CFLAGS="${CFLAGS} -ggdb -O0"
    fi

    emake CFLAGS="${CFLAGS}" -j1 || die "compile failed"
}

src_install() {
    emake -j1 DESTDIR="${D}" install || die "install failed"
    dodoc design.txt readme.txt sesman/startwm.sh
    newinitd "${FILESDIR}/${PN}.initd" ${PN}
}
